package be.kuleuven.peno3.mobiletoledo.Data.Client;

import java.io.IOException;

import org.apache.commons.httpclient.HttpException;
import org.json.JSONException;
import org.json.JSONObject;

import be.kuleuven.peno3.mobiletoledo.model.Question;
import be.kuleuven.peno3.mobiletoledo.model.Result;

import com.google.gson.Gson;

public abstract class MultipleChoiceClient extends Client{
	
	public  static Question[] getQuestions(int course){
		try {
			String json = stringOfUrl(host+"/QuestionHandler/getQuestions?course_id="+course);
			
			Question[] questions =new Gson().fromJson(json, Question[].class);
			
			return questions;
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
			return null;
		} catch (HttpException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	//hulpklasse om daarna 1 Result klasse te kunnen aanmaken via Json die alle id's bevat
	private static class Answer{private int answerid, counter;}
	
	public  static Result getResult(int questionid){
		try {
			String json = stringOfUrl(host+"/QuestionHandler/getAnswers?questionid="+questionid);
			
			Answer[] r =new Gson().fromJson(json, Answer[].class);
			int[] a = new int[r.length];
			for(int i=0;i<r.length;i++) {
				a[r[i].answerid]=r[i].counter;
			}
			Result result=new Result(a);
			
			return result;
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
			return null;
		} catch (HttpException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static boolean answerQuestion(int questionid, int answerid) {
		try {
			String json = stringOfUrl(host+"/QuestionHandler/addAnswer?questionid="+questionid+"&answerid="+answerid);
			
			String result = null;
			try {
				result = (String) new JSONObject(json).get("result");
			} catch (JSONException e) {
				e.printStackTrace();
				return false;
			}
			
			if(result.indexOf("successful")>0) {
				return true;
			}
			return false;
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public  static Question getQuestion(int id){
		try {
			String json = stringOfUrl(host+"/QuestionHandler/getQuestion?id="+id);
			
			Question[] question =new Gson().fromJson(json, Question[].class);
			
			return question[0];
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
			return null;
		} catch (HttpException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static boolean deleteQuestion(int id) {
		try {
			String json = stringOfUrl(host+"/QuestionHandler/deleteQuestion?id="+id);
			
			String result = null;
			try {
				result = (String) new JSONObject(json).get("result");
			} catch (JSONException e) {
				e.printStackTrace();
				return false;
			}
			
			if(result.indexOf("successful")>0) {
				return true;
			}
			return false;
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	
	public static boolean addQuestion(String content, String author, int course_id, String possible_answers) {
		try {
			String url="content="+content+"&author="+author+"&course_id="+course_id+"&possible_answers="+possible_answers;
			url=encode(url);
			url=host+"/QuestionHandler/addQuestion?"+url;
			String json = stringOfUrl(url);
			
			String result = null;
			try {
				result = (String) new JSONObject(json).get("result");
			} catch (JSONException e) {
				e.printStackTrace();
				return false;
			}
			
			if(result.indexOf("successful")>0) {
				return true;
			}
			return false;
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}
}
